package com.tools.web.work.captcha;

import com.tools.common.object.Note;

import java.util.Objects;

/**
 * 验证码结果包装类：
 * 主要包装了验证码的允许错误次数，和已错误的次数
 * 用于做校验
 * */
@Note("验证码结果包装类：" +
        "主要包装了验证码的允许错误次数，和已错误的次数用于做校验")
public class CaptchaBean {

    @Note("验证码结果")
    private Object captcha;

    @Note("允许错误的次数")
    private int allow = 3;

    @Note("已经错误的次数")
    private int incorrect;

    /* ********************************************************************************************
     *
     *          构造器
     *
     * *********************************************************************************************
     * */

    private CaptchaBean() {}

    public CaptchaBean(Object captcha) {
        this.setCaptcha(captcha);
    }

    public CaptchaBean(Object captcha, int allow, int incorrect) {
        this.setCaptcha(captcha);
        this.setAllow(allow);
        this.setIncorrect(incorrect);
    }

    public static CaptchaBean of(Object captcha) {
        return new CaptchaBean(captcha);
    }

    public static CaptchaBean of(Object captcha, int allow, int incorrect) {
        return new CaptchaBean(captcha, allow, incorrect);
    }

    /* ********************************************************************************************
     *
     *          Getter / Setter
     *
     * *********************************************************************************************
     * */

    public Object getCaptcha() {
        return captcha;
    }

    public void setCaptcha(Object captcha) {
        if(captcha == null) throw new NullPointerException("验证码结果为 null");
        this.captcha = captcha;
    }

    public int getAllow() {
        return allow;
    }

    public void setAllow(int allow) {
        this.allow = Math.max(allow, 1);
    }

    public int getIncorrect() {
        return incorrect;
    }

    public void setIncorrect(int incorrect) {
        this.incorrect = incorrect;
    }

    public void incrIncorrect() {
        this.incorrect++;
    }

    public boolean reachedThreshold() {
        return this.incorrect >= this.allow;
    }

    @Override
    public String toString() {
        return "CaptchaBean{" +
                "allow=" + allow +
                ", incorrect=" + incorrect +
                ", captcha=" + captcha +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        CaptchaBean that = (CaptchaBean) o;
        return allow == that.allow && incorrect == that.incorrect && Objects.equals(captcha, that.captcha);
    }

    @Override
    public int hashCode() {
        return Objects.hash(captcha, allow, incorrect);
    }

    public CaptchaBean captcha(Object captcha) {
        this.setCaptcha(captcha);
        return this;
    }

    public CaptchaBean allow(int allow) {
        this.setAllow(allow);
        return this;
    }

    public CaptchaBean incorrect(int incorrect) {
        this.setIncorrect(incorrect);
        return this;
    }
}
